package com.zcsy.manage.web.mall;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.manage.model.mall.IntegralCategory;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.mall.IIntegralCategoryService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.web.common.LogUtils;

/**
 * @Title: IntegralCategoryController.java
 * @Package： com.zcsy.manage.web.mall
 * @Description: 积分分类管理控制层管理类
 * @author liuying 1075817474@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年4月14日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/integralCategory")
public class IntegralCategoryController {

	@Autowired
	private IIntegralCategoryService integralCategoryService;

	@Autowired
	private IDictService dictService;
	
	/**
	 * @Description 分页获取数据列表<br>
	 * @param pNo
	 *            起始页
	 * @param integralName
	 *            名称
	 * @param req
	 *            用户请求
	 * @return 跳转页面URL
	 * @author liuying 1075817474@qq.com
	 * @date 2016年4月14日
	 * @see
	 */
	@RequestMapping(value = "integralCategoryList", method = RequestMethod.GET)
	public String integralCategoryList(Model model, Integer pNo, Integer pSize, 
			String integralName, HttpServletRequest req) {
		if (pNo == null) {
			pNo = Constants.PAGE_NO;
		}

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		// 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
		PageHelper.startPage(pNo, pageSize);

		IntegralCategory integralCategory = new IntegralCategory();

		// 名称不为空时，设置名称
		if (StringUtils.isNotEmpty(integralName)) {
			integralCategory.setName(integralName);
		}

		List<IntegralCategory> integralCategorys = integralCategoryService
				.selectByKeyword(integralCategory);
		PageInfo<IntegralCategory> info = new PageInfo<IntegralCategory>(
				integralCategorys);

		model.addAttribute("integralCategory", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);
		model.addAttribute("integralName", integralName);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

		return "mall/integralcategory/integralCategoryList";
	}

	/**
	 * @Description 编辑积分分类管理<br>
	 * @param id
	 *            主键id
	 * @return 跳转画面URL
	 * @author liuying 1075817474@qq.com
	 * @date 2016年4月14日
	 * @see
	 */
	@RequestMapping(value = "editIntegralCategory", method = RequestMethod.GET)
	public String editIntegralCategory(Model model, String id) {
		try {
			if (!String.valueOf(Constants.STATE_DISABLED).equals(id)) {
				IntegralCategory integralCategory = integralCategoryService
						.selectByPrimaryKey(id);
				model.addAttribute("integralCategory", integralCategory);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "mall/integralcategory/integralCategoryEdit";
	}

	/**
	 * @Description 保存或更新积分分类管理<br>
	 * @param integralName
	 *            名称
	 * @param min
	 *            积分最小值
	 * @param max
	 *            积分最大值
	 * @param categoryId
	 *            主键id
	 * @param request
	 *            用户请求
	 * @param response
	 *            响应
	 * @return 请求结果
	 * @author liuying 1075817474@qq.com
	 * @date 2016年4月14日
	 * @see
	 */
	@RequestMapping(value = "saveOrUpdateCategory", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson saveOrUpdateCategory(String integralName, Integer min,
			Integer max, String categoryId, String logo, HttpServletRequest request,
			HttpServletResponse response) {
		RequestJson result = new RequestJson();
		User user = (User) request.getSession().getAttribute(
				RequestConstants.USER_SESSION_ID);
		try {
			IntegralCategory record = new IntegralCategory();
			record.setName(integralName);
			record.setMin(min);
			record.setMax(max);
			record.setUserAccount(user.getLoginname());
			record.setLogo(logo);

			if (categoryId != null && !"".equals(categoryId)) {
				record.setId(categoryId);
				record.setRecordTime(new Date());
				integralCategoryService.update(record);
				LogUtils.insertLog("用户编辑积分信息", user.getId(), request);

			} else {
				record.setId(UniqueNumberUtils.get18UniqueNumber());
				record.setRecordTime(new Date());
				integralCategoryService.insert(record);
			    LogUtils.insertLog("用户新增积分信息", user.getId(), request);
			}

		} catch (Exception e) {
			result.setSuccess(false);
		}
		
		return result;
	}

	/**
	 * @Description 按照id删除数据<br>
	 * @param iDs
	 *            多个id
	 * @param req
	 *            用户请求
	 * @return 请求结果
	 * @author liuying 1075817474@qq.com
	 * @date 2016年4月14日
	 * @see
	 */
	@RequestMapping(value = "deleteCategory", method = RequestMethod.POST)
	@ResponseBody
	public RequestJson deleteCategory(String iDs, HttpServletRequest req) {

		RequestJson result = new RequestJson();
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);

		try {
			String ids[] = iDs.split("\\,");
			for (int i = 0; i < ids.length; i++) {
				integralCategoryService.delete(ids[i]);
			}
			
			result.setSuccess(true);
			LogUtils.insertLog("用户删除积分信息", user.getId(), req);

		} catch (NumberFormatException e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}
		
		return result;
	}
}
